const express = require('express');

const router = express.Router();

const teacherArray = [
    { id: 1001 , name: '孙老师' , gender: '靓仔' , married: '已婚' } ,
    { id: 1002 , name: '马老师' , gender: '靓仔' , married: '未婚' } ,
    { id: 1003 , name: '宋老师' , gender: '靓妹' , married: '已婚' }
]

router.get( '/list' , (req,resp) => {
    let viewName = 'teacher/list' ;
    let localVariable = { 
        teachers: teacherArray ,
        message: '<p>这他娘的秦腔算个屁</p>' ,
        msg : undefined
    }
    resp.render( viewName , localVariable , ( err , html ) => {
        if( err ) {
            console.log( err );
            resp.send( '<div>模板文件渲染失败</div>' );
            return ;
        }
        resp.send( html );
    });
});

router.get( '/add' , (req,resp) => {

    let view = 'teacher/add' ;

    let locals = {
        // message: '<p>这是添加教师的页面</p>' 
        message: `<script type="text/javascript">
                    alert( '我执行了' );
                  </script>`
    }

    let cb = (err,html) => {
        if( !err ) {
            resp.send( html );
        }
    }

    resp.render( view , locals , cb );
});

router.get( '/edit/:id' , (req,resp) => {

    let id = req.params.id ;

    let t = teacherArray.find( t => t.id == id );

    resp.render( 'teacher/edit' , {teacher: t } , (err,html)=>{
        if( !err ){
            resp.send( html );
        }
    });
});


module.exports = router ;